package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.MsgResult;
import com.itheima.common.R;
import com.itheima.common.Series;
import com.itheima.dao.*;
import com.itheima.dto.*;
import com.itheima.pojo.*;
import com.itheima.service.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class StatisticsServieImp implements StatisticsService {
    @Resource
    private TbContractDao tbContractDao;

    @Resource
    private TbActivityDao tbActivityDao;

    @Resource
    private SysDictDataDao sysDictDataDao;

    @Resource
    private TbClueDao tbClueDao;

    @Resource
    private TransferDao transferDao;

    @Resource
    private TbAssignRecordDao tbAssignRecordDao;



    //新增客户统计--坐标
    //销售统计和客户统计基于同一个表查询
    //注意:客户名字可以重复
    @Override
    public R contractStatistics(Date startTime, Date endTime) {

        if (startTime == null||endTime==null) {
            return R.error("输入参数不合法!");
        }
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        //1.横坐标:日期坐标集合
        List<String> timeList =new ArrayList<>();
        //2.纵坐标:
        List<Series> seriesList=new ArrayList<>();
        List<Integer> seriesListNow=new ArrayList<>();
        List<Integer> seriesListSum=new ArrayList<>();
        Series seriesNow=new Series();
        seriesNow.setName("新增客户数量");
        Series seriesSun=new Series();
        seriesSun.setName("客户总数");
        Integer contractNow;
        Integer contractSum=0;
        while (startTime.getTime()<=endTime.getTime()) {
            String startTimeStr1 = sdf.format(startTime);
            log.info("startTimeStr1:{}",startTimeStr1);
            timeList.add(startTimeStr1);//
            Date nextTime=new Date();
            nextTime.setTime(startTime.getTime() + 3600 * 24 * 1000);
            if (tbContractDao.getByTime(startTime,nextTime) == null) {
                contractNow=0;
            }else {
                contractNow=tbContractDao.getByTime(startTime,nextTime);
            }
            //添加对应纵坐标--新增客户数量
            seriesListNow.add(contractNow);
            System.err.println("seriesListNow:"+seriesListNow);
            contractSum+=contractNow;
            //添加对应纵坐标--客户总数
            seriesListSum.add(contractSum);
            System.err.println("seriesListSum:"+seriesListSum);
            startTime.setTime(startTime.getTime()+3600*24*1000);
        }
        seriesNow.setData(seriesListNow);
        seriesSun.setData(seriesListSum);
        seriesList.add(seriesNow);
        seriesList.add(seriesSun);
        CoordinateDto coordinateDto=new CoordinateDto();
        coordinateDto.setXAxis(timeList);
        coordinateDto.setSeriesList(seriesList);

        return R.success("客户坐标封装体",coordinateDto);
    }
    //客户学科分布
    @Override
    public R contractBySubject(Date startTime, Date endTime) {
        //找出所有合同涉及的学科id放入返回值集合
        //id:num
        Map<Long,Integer> map=new HashMap();

        LambdaQueryWrapper<TbContract> lqw=new LambdaQueryWrapper<>();
        lqw.between(TbContract::getCreateTime,startTime,endTime);
        lqw.eq(TbContract::getStatus,4);//已完成合同
        List<TbContract> tbContracts = tbContractDao.selectList(lqw);
        for (int i = 0; i < tbContracts.size(); i++) {
            Long activityId = tbContracts.get(i).getActivityId();
            //存在则+1不存在则初始化
            if (map.containsKey(activityId)) {
                Integer num1 = map.get(activityId);
                map.put(activityId,num1+1);
            }else {
                map.put(activityId,1);
            }
        }
        //把map克隆到对象集合
        List<ActivityPieDto> activityPieDtos1=new ArrayList<>();
        map.forEach((activityId,num)->{
            ActivityPieDto activityPieDto = new ActivityPieDto(null,num,activityId);
            activityPieDto.setActivity(tbActivityDao.selectNameById(activityId));
            activityPieDtos1.add(activityPieDto);
        });
        return R.success("合同活动比例封装体",activityPieDtos1);
    }

    //合同销售金额统计--坐标
    @Override
    public R ordersStatisticsXy(Date startTime, Date endTime) {

    //计算时间范围内成交合同总金额
        if (startTime == null||endTime==null) {
            return R.error("输入参数不合法!");
        }
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        //1.横坐标:日期坐标集合
        List<String> timeList =new ArrayList<>();
        //2.纵坐标:成交合同数量整数集合
        List<Double> seriesListNow=new ArrayList<>();
        Series seriesNow=new Series();
        while (startTime.getTime()<=endTime.getTime()) {
            Double contractNow =0d;
            String startTimeStr1 = sdf.format(startTime);
            log.info("startTimeStr1:{}",startTimeStr1);
            timeList.add(startTimeStr1);//
            Date nextTime=new Date();
            nextTime.setTime(startTime.getTime() + 3600 * 24 * 1000);
            //getByTimeAndStatus
            String status="4";
            if (tbContractDao.getContractOrderByTimeAndStatus(startTime,nextTime,status) == null) {
                contractNow=0d;
            }else {
                List <Double> money1=tbContractDao.getContractOrderByTimeAndStatus(startTime,nextTime,status);
                for (int i = 0; i < money1.size(); i++) {

                    contractNow+=money1.get(i);
                }
            }
            //添加对应纵坐标--新增客户数量
            seriesListNow.add(contractNow);
            System.err.println("seriesListNow:"+seriesListNow);
            //日期++
            startTime.setTime(startTime.getTime()+3600*24*1000);
        }
        seriesNow.setName("销售金额统计纵坐标1");
        seriesNow.setData(seriesListNow);//正整数集合放到纵坐标封装对象
        //添加封装纵坐标对象到坐标封装体中纵坐标集合
        List<Series> seriesList1=new ArrayList<>();
        seriesList1.add(seriesNow);
        CoordinateDto coordinateDto=new CoordinateDto();
        coordinateDto.setXAxis(timeList);
        coordinateDto.setSeriesList(seriesList1);



        return R.success("销售额坐标封装体",coordinateDto);
    }

    //成交合同数量统计--坐标
    @Override
    public R contractByStatusXy(Date startTime, Date endTime) {
        //计算时间范围内成交合同数量
        //查询指定时间范围内合同集合(已经成交??待定)
        if (startTime == null||endTime==null) {
            return R.error("输入参数不合法!");
        }
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        //1.横坐标:日期坐标集合
        List<String> timeList =new ArrayList<>();
        //2.纵坐标:成交合同数量整数集合
        List<Integer> seriesListNow=new ArrayList<>();
        Series seriesNow=new Series();
        Integer contractNow;
        while (startTime.getTime()<=endTime.getTime()) {
            String startTimeStr1 = sdf.format(startTime);
            log.info("startTimeStr1:{}",startTimeStr1);
            timeList.add(startTimeStr1);//
            Date nextTime=new Date();
            nextTime.setTime(startTime.getTime() + 3600 * 24 * 1000);
            //getByTimeAndStatus
            String status="4";
            if (tbContractDao.getByTimeAndStatus(startTime,nextTime,status) == null) {
                contractNow=0;
            }else {
                contractNow=tbContractDao.getByTimeAndStatus(startTime,nextTime,status);
            }
            //添加对应纵坐标--新增客户数量
            seriesListNow.add(contractNow);
            System.err.println("seriesListNow:"+seriesListNow);
            //日期++
            startTime.setTime(startTime.getTime()+3600*24*1000);
        }
        seriesNow.setName("销售合同统计纵坐标1");
        seriesNow.setData(seriesListNow);//正整数集合放到纵坐标封装对象
        //添加封装纵坐标对象到坐标封装体中纵坐标集合
        List<Series> seriesList1=new ArrayList<>();
        seriesList1.add(seriesNow);
        CoordinateDto coordinateDto=new CoordinateDto();
        coordinateDto.setXAxis(timeList);
        coordinateDto.setSeriesList(seriesList1);



        return R.success("成交合同坐标封装体",coordinateDto);
    }



    //线索统计--坐标
    //1.新增线索数量 2.线索新增总量
    @Override
    public R cluesStatistics(Date startTime, Date endTime) {
        if (startTime == null||endTime==null) {
            return R.error("输入参数不合法!");
        }
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
        //1.横坐标:日期坐标集合
        List<String> timeList =new ArrayList<>();
        //2.纵坐标:
        List<Series> seriesList=new ArrayList<>();
        List<Integer> seriesListNow=new ArrayList<>();
        List<Integer> seriesListSum=new ArrayList<>();
        Series seriesNow=new Series();
        seriesNow.setName("新增线索数量");
        Series seriesSun=new Series();
        seriesSun.setName("线索总数");
        Integer cluesNow;
        Integer cluesSum=0;
        while (startTime.getTime()<=endTime.getTime()) {
            String startTimeStr1 = sdf.format(startTime);
            log.info("startTimeStr1:{}",startTimeStr1);
            timeList.add(startTimeStr1);//
            Date nextTime=new Date();
            nextTime.setTime(startTime.getTime() + 3600 * 24 * 1000);
            if (tbClueDao.getByTime(startTime,nextTime)==null) {
                cluesNow=0;
            }else {
                cluesNow=tbClueDao.getByTime(startTime,nextTime);
            }
            //添加对应纵坐标--新增客户数量
            seriesListNow.add(cluesNow);
            System.err.println("seriesListNow:"+seriesListNow);
            cluesSum+=cluesNow;
            //添加对应纵坐标--客户总数
            seriesListSum.add(cluesSum);
            System.err.println("seriesListSum:"+seriesListSum);
            startTime.setTime(startTime.getTime()+3600*24*1000);
        }
        seriesNow.setData(seriesListNow);
        seriesSun.setData(seriesListSum);
        seriesList.add(seriesNow);
        seriesList.add(seriesSun);
        CoordinateDto coordinateDto=new CoordinateDto();
        coordinateDto.setXAxis(timeList);
        coordinateDto.setSeriesList(seriesList);

        return R.success("线索坐标封装体",coordinateDto);
    }

    //3.线索转化率:查找指定时间范围内线索去向
    //1.线索总数 2.有效线索 3.商机转化 4.转化客户
    //默认商机和客户全由线索转化而来
    //线索转化状态 transfer :0线索默认  1:转化为商机,转化为商机新增商机表
    //转化为商机后无法跟进转化信息
    //新建一个转化表 记录转化过程 transfer
    //线索转化商机:transfer type:1
    //线索转化客户:transfer type:2
    //有效线索:去除伪线索

    //线索转化率--漏斗图(对应比例)
    @Override
    public R cluesTransfer(Date startTime, Date endTime) {
        Map<String,Integer> map =new HashMap();
        //1.查询所有线索
        Integer byTime=0;
        if (tbClueDao.getByTime(startTime, endTime) != null) {
            byTime = tbClueDao.getByTime(startTime, endTime);
        }
        map.put("线索总数",byTime);

        //2.查询所有线索 伪线索除外
        String status="4";
        Integer clueCount1=0;
        if (tbClueDao.selectCountNotFByTime(startTime,endTime,status) != null) {
            clueCount1=tbClueDao.selectCountNotFByTime(startTime,endTime,status);
        }
        map.put("有效线索",clueCount1);

        //3.由线索转化的商机
        String type1="1";
        Integer businessTransfer=0;
        if (transferDao.selectCountByTimeAndType(startTime, endTime,type1)!= null) {
            businessTransfer =transferDao.selectCountByTimeAndType(startTime, endTime,type1);
        }
        map.put("转化商机",businessTransfer);
        //4.由线索转化(先转化为商机)的合同
        String type2="2";
        Integer contractTransfer=0;
        if (transferDao.selectCountByTimeAndType(startTime, endTime,type2)!= null) {

            contractTransfer=transferDao.selectCountByTimeAndType(startTime, endTime,type2);
        }
        map.put("转化客户",contractTransfer);

        return R.success("线索转化率数据",map);
    }

    //1.渠道:指定时间合同,渠道占比--饼图统计
    @Override
    public R channelStatistics(Date startTime, Date endTime) {
//找出所有合同涉及的id放入返回值集合
        //id:num
        Map<String,Integer> map=new HashMap();

        LambdaQueryWrapper<TbContract> lqw=new LambdaQueryWrapper<>();
        lqw.between(TbContract::getCreateTime,startTime,endTime);
        lqw.eq(TbContract::getStatus,4);//已完成合同
        List<TbContract> tbContracts = tbContractDao.selectList(lqw);
        for (int i = 0; i < tbContracts.size(); i++) {
            String channel = tbContracts.get(i).getChannel();
            //存在则+1不存在则初始化
            if (map.containsKey(channel)) {
                Integer num1 = map.get(channel);
                map.put(channel,num1+1);
            }else {
                map.put(channel,1);
            }
        }
        //把map克隆到对象集合
        List<ChannelPieDto> channelPieDtos=new ArrayList<>();
        map.forEach((channel,num)->{
            String channelName = sysDictDataDao.selectByDictTypeAndValue("channel_type",channel);
            ChannelPieDto channelPieDto=new ChannelPieDto(num,channelName);
            channelPieDtos.add(channelPieDto);
        });

        return R.success("合同渠道比例封装体",channelPieDtos);
    }

    //活动:指定合同时间 活动占比--饼图
    @Override
    public R activityStatistics(Date startTime, Date endTime) {
        //找出所有合同涉及的id放入返回值集合
        //id:num
        Map<Long,Integer> map=new HashMap();

        LambdaQueryWrapper<TbContract> lqw=new LambdaQueryWrapper<>();
        lqw.between(TbContract::getCreateTime,startTime,endTime);
        lqw.eq(TbContract::getStatus,4);//已完成合同
        List<TbContract> tbContracts = tbContractDao.selectList(lqw);
        for (int i = 0; i < tbContracts.size(); i++) {
            Long activityId = tbContracts.get(i).getActivityId();
            //存在则+1不存在则初始化
            if (map.containsKey(activityId)) {
                Integer num1 = map.get(activityId);
                map.put(activityId,num1+1);
            }else {
                map.put(activityId,1);
            }
        }
        //把map克隆到对象集合
        List<ActivityPieDto> activityPieDtos1=new ArrayList<>();
        map.forEach((activityId,num)->{
            ActivityPieDto activityPieDto = new ActivityPieDto(null,num,activityId);
            activityPieDto.setActivity(tbActivityDao.selectNameById(activityId));
            activityPieDtos1.add(activityPieDto);
        });
        return R.success("合同活动比例封装体",activityPieDtos1);
    }



    //1.客户统计分页
    //搜索条件 时间 所属渠道  归属部门 归属人
    //回显条目 客户id 归属人姓名 学科 课程 渠道 归属人 成交时间
    @Override
    public R contractStatisticsPage(ContractStatisticsDto contractStatisticsDto) {

        IPage<TbContract> p = new Page<>(contractStatisticsDto.getPageNum(),contractStatisticsDto.getPageSize());
        LambdaQueryWrapper<TbContract> wrapper= new LambdaQueryWrapper<>();
        List<TbContract> wrapper1=new ArrayList<>();
        //创建时间
        wrapper.between(
                contractStatisticsDto.getBeginCreateTime()!=null
                        &&contractStatisticsDto.getEndCreateTime()!=null,
                TbContract::getCreateTime,contractStatisticsDto.getBeginCreateTime(),
                contractStatisticsDto.getEndCreateTime());
        //渠道
        wrapper.eq(contractStatisticsDto.getChannel()!=null,
                TbContract::getChannel,contractStatisticsDto.getChannel());
        //归属部门
        wrapper.eq(contractStatisticsDto.getDeptId()!=null,
                TbContract::getDeptId,contractStatisticsDto.getDeptId()
        );
        //归属人
        wrapper.eq(contractStatisticsDto.getUserId()!=null,
                TbContract::getUserId,contractStatisticsDto.getUserId()
        );

        IPage<TbContract> tbContractIPage = tbContractDao.selectPage(p, wrapper);
        return new R(1, MsgResult.SUCCESS_FIND,tbContractIPage);
    }


    //2.销售统计分页
    //搜索条件 1.时间 2.所属渠道 3.归属人 4.归属部门 5.排序规则(归属渠道 归属部门 归属人)
    //回显条目 1.排行 2.归属渠道/归属部门/归属人 3.合同数量 4.成交金额
    @Override
    public R ordersStatisticsPage(OrdersContractStatisticsDto ordersContractStatisticsDto) {
        String orderA = ordersContractStatisticsDto.getOrderA();
        if (orderA == null) {
            return R.error("输入不合法");
        }
        if (orderA.equals("归属渠道")) {
            //获得查询分页主体 再克隆 往里添加数据
            IPage<SysDictData> pageInfo=new Page<>(
                    ordersContractStatisticsDto.getPageNum(),ordersContractStatisticsDto.getPageSize());
            LambdaQueryWrapper<SysDictData> lqw=new LambdaQueryWrapper<>();
            lqw.eq(SysDictData::getDictType,"clues_item");
            sysDictDataDao.selectPage(pageInfo,lqw);
            //克隆
            IPage<OrdersReturnDto> ordersPageInfo=new Page<>();
            //克隆分页对象
            BeanUtils.copyProperties(pageInfo,ordersPageInfo);
            List<SysDictData> records = pageInfo.getRecords();
            //克隆内容属性
            String s = JSON.toJSONString(records);
            List<OrdersReturnDto> ordersReturnDtos = JSON.parseArray(s, OrdersReturnDto.class);
            //处理集合 补全信息
            ordersReturnDtos.forEach(ordersReturnDto -> {
                List<TbContract> contracts=tbContractDao.selectByChannel(ordersReturnDto.getDictValue());//渠道键值
                numAndMoneyStatistic(ordersReturnDto, contracts);
            });
            //克隆内容放入分页信息
            ordersPageInfo.setRecords(ordersReturnDtos);
            return R.success("归属渠道",ordersPageInfo);
        }
        if (orderA.equals("归属部门")) {
            //获得查询分页主体 再克隆 往里添加数据
            IPage<TbContract> pageInfo=new Page<>(
                    ordersContractStatisticsDto.getPageNum(),ordersContractStatisticsDto.getPageSize());
            LambdaQueryWrapper<TbContract> lqw=new LambdaQueryWrapper<>();
            lqw.eq(TbContract::getDeptId,ordersContractStatisticsDto.getDeptId());
            tbContractDao.selectPage(pageInfo,lqw);
            //克隆
            IPage<OrdersReturnDto> ordersPageInfo=new Page<>();
            //克隆分页对象
            BeanUtils.copyProperties(pageInfo,ordersPageInfo);
            List<TbContract> records = pageInfo.getRecords();
            //克隆内容属性
            String s = JSON.toJSONString(records);
            List<OrdersReturnDto> ordersReturnDtos = JSON.parseArray(s, OrdersReturnDto.class);
            //处理集合 补全信息

            ordersReturnDtos.forEach(ordersReturnDto -> {
                List<TbContract> contracts=tbContractDao.selectByDeptId(ordersReturnDto.getDeptId());
                numAndMoneyStatistic(ordersReturnDto, contracts);
            });
            //克隆内容放入分页信息
            ordersPageInfo.setRecords(ordersReturnDtos);
            return R.success("归属部门",ordersPageInfo);
        }
        if (orderA.equals("归属人")) {
            //获得查询分页主体 再克隆 往里添加数据
            IPage<TbContract> pageInfo=new Page<>(
                    ordersContractStatisticsDto.getPageNum(),ordersContractStatisticsDto.getPageSize());
            LambdaQueryWrapper<TbContract> lqw=new LambdaQueryWrapper<>();
            lqw.eq(TbContract::getUserId,ordersContractStatisticsDto.getUserId());
            tbContractDao.selectPage(pageInfo,lqw);
            //克隆
            IPage<OrdersReturnDto> ordersPageInfo=new Page<>();
            //克隆分页对象
            BeanUtils.copyProperties(pageInfo,ordersPageInfo);
            List<TbContract> records = pageInfo.getRecords();
            //克隆内容属性
            String s = JSON.toJSONString(records);
            List<OrdersReturnDto> ordersReturnDtos = JSON.parseArray(s,OrdersReturnDto.class);
            //处理集合 补全信息
            ordersReturnDtos.forEach(ordersReturnDto -> {
                List<TbContract> contracts=tbContractDao.selectByUserId(ordersReturnDto.getUserId());
                numAndMoneyStatistic(ordersReturnDto, contracts);
            });
            //克隆内容放入分页信息
            ordersPageInfo.setRecords(ordersReturnDtos);
            return R.success("归属人",ordersPageInfo);
        }
        return R.error("请求参数不合法");
    }

    private void numAndMoneyStatistic(OrdersReturnDto ordersReturnDto, List<TbContract> contracts) {
        if (contracts != null) {
            ordersReturnDto.setContractNum(contracts.size());
            Integer sumMoney=0;
            for (int i = 0; i < contracts.size(); i++) {
                sumMoney+=(int) (contracts.get(i).getContractOrder()*100);
            }
            ordersReturnDto.setMoney(sumMoney);
        }else {
            ordersReturnDto.setContractNum(0);
            ordersReturnDto.setMoney(0);
        }
    }

    //3.线索统计分页
    //搜索条件 1.时间 2.线索id 3.客户姓名 4.归属人 5.部门
    //回显条目 1.线索编号 2.手机号 3客户姓名 4.归属人部门 5.归属人 6.渠道 7.活动 8.线索状态 9.创建时间
    @Override
    public R cluesStatisticsPage(ClueQueryDto clueQueryDto) {
        IPage<TbClue> pageInfo=new Page<>(clueQueryDto.getPage(),clueQueryDto.getPageSize());
        LambdaQueryWrapper<TbClue> lqw=new LambdaQueryWrapper<>();
        //创建时间
        lqw.between(clueQueryDto.getStartTime()!=null &&clueQueryDto.getEndTime()!=null,
                TbClue::getCreateTime,clueQueryDto.getStartTime(), clueQueryDto.getEndTime()
                );
        //2.线索id
        lqw.like(clueQueryDto.getId()!=null,TbClue::getId,clueQueryDto.getId());
        //3.客户姓名
        lqw.like(clueQueryDto.getName()!=null,TbClue::getName,clueQueryDto.getName());
        //4.归属人
        if (clueQueryDto.getUserId()!=null) {
            lqw.eq(TbClue::getUserId,clueQueryDto.getUserId());
        }
        //5.部门
        if (clueQueryDto.getDeptId()!=null) {
            lqw.eq(TbClue::getDeptId,clueQueryDto.getDeptId());
        }
        tbClueDao.selectPage(pageInfo,lqw);
        return R.success("线索统计分页",pageInfo);
    }

    //4.渠道(活动)统计分页
    //搜索条件 1.时间 2.活动编号 3.活动名称 (4.归属人 5.归属部门)
    //回显条目 1.渠道 2.活动编号 3.活动名称 4.线索总数 5.转化商机数 6.转化客户数 7.成交金额 8.活动成本
    @Override
    public R channelStatisticsPage(ActivityStatictisticsSouDto activityStatictisticsSouDto) {
        IPage<TbActivity> pageInfo = new Page<>(activityStatictisticsSouDto.getPageNum(),activityStatictisticsSouDto.getPageSize());
        LambdaQueryWrapper<TbActivity> wrapper= new LambdaQueryWrapper<>();
        //1.创建时间
        wrapper.between(
                activityStatictisticsSouDto.getBeginCreateTime()!=null
                        &&activityStatictisticsSouDto.getEndCreateTime()!=null,
                TbActivity::getCreateTime,activityStatictisticsSouDto.getBeginCreateTime(),
                activityStatictisticsSouDto.getEndCreateTime());
        //2.活动编号
        wrapper.eq(activityStatictisticsSouDto.getId()!=null,
                TbActivity::getId,activityStatictisticsSouDto.getId());
        //3.活动名称
        wrapper.eq(activityStatictisticsSouDto.getName()!=null,
                TbActivity::getName,activityStatictisticsSouDto.getName());
        tbActivityDao.selectPage(pageInfo,wrapper);

        //克隆
        IPage<ActivityStatisticsReturnDto> activityReturnDtoPage=new Page<>();
        //克隆分页对象
        BeanUtils.copyProperties(pageInfo,activityReturnDtoPage);
        List<TbActivity> records = pageInfo.getRecords();
        //克隆内容属性
        String s = JSON.toJSONString(records);
        List<ActivityStatisticsReturnDto> activityStatisticsReturnDtos
                = JSON.parseArray(s, ActivityStatisticsReturnDto.class);
        //处理集合 补全信息
        //4.线索总数 5.转化商机数 6.转化客户数 7.成交金额 8.活动成本
        activityStatisticsReturnDtos.forEach(returnDto -> {
            //4.线索总数
            List<TbClue> tbClues=tbClueDao.selectByActivityId(returnDto.getId());
            returnDto.setClueCount(tbClues.size());
            List<TbClue> busines=new ArrayList<>();
            List<TbClue> constracts=new ArrayList<>();
            tbClues.forEach(tbClue -> {
                //5.转化商机数:transfer : 1
                if (tbClue.getId().equals("1")) {
                    busines.add(tbClue);
                }
                //6.转化客户数:
                if (tbClue.getId().equals("2")) {
                    constracts.add(tbClue);
                }
            });
            returnDto.setClueToBusinessCount(busines.size());
            returnDto.setClueToContractCount(constracts.size());
            //7.成交金额
            Double sum=0d;
            for (int i = 0; i < constracts.size(); i++) {
                Long contractId=transferDao.selectContractIdByClueId(constracts.get(i).getId());
                TbContract tbContract = tbContractDao.selectById(contractId);
                if (tbContract.getStatus().equals("4")) {
                    sum+=tbContract.getContractOrder();
                }
            }
            returnDto.setAmount(sum);
            //8.活动成本
            returnDto.setActivityAmount((1-returnDto.getDiscount())*sum);

        });
        //
        //克隆内容放入分页信息
        activityReturnDtoPage.setRecords(activityStatisticsReturnDtos);
        return R.success("线索统计分页",activityReturnDtoPage);

    }

}
